/**
 * Switcher widget creator.
 * 
 * Implements a switcher by using CSS classes: switcher-view, switcher-show, switcher-hide
 */

function SwitcherWidget(options) {
	if(!options) options = {};
	if(!options.id) options.id = Widget.uid();

	var viewClazz = "switcher-view";

	var that = Widget(options);

	function getViews() {
		return that.getContainer().find("." + viewClazz);
	}
	
	that.getHtml = function() {
		return sprintf('<div id="%s"></div>', options.id);
	};

	function viewId(index) {
		return options.id + "-view-" + index;
	}
	
	function nextViewId() {
		return viewId(getViews().size());
	}

	that.size = function() {
		return getViews().size();
	};

	that.addView = function(content) {
		that.getContainer().append(sprintf('<div class="%s switcher-hide" id="%s">%s</div>', viewClazz, nextViewId(), content));
	};
	
	that.removeViews = function(index) {
		getViews().slice(index).remove();
	};
	
	that.showView = function(index) {
		mwl.setGroupTarget('#' + options.id, '#' + viewId(index), 'switcher-show', 'switcher-hide');
	};
	
	that.linkMwl = function(index) {
		return sprintf("mwl.setGroupTarget('#%s', '#%s', 'switcher-show', 'switcher-hide');mwl.toggleClass('#top', 'topblock');mwl.scrollTo('#top');mwl.toggleClass('#top', 'topblock');", options.id, viewId(index));
	};
	
	return that;
}
